<?php
	set_include_path (PHPPATH);
	require_once 'MDB2.php';
	/**
	 * 
	 * The category controller
	 * @author Tri
	 *
	 */
	class category extends Application 
	{
		/**
		 * 
		 * Constructor
		 */
		function __construct(&$tmpl)
		{			
			$this->tmpl = &$tmpl;		
		}

		/**
		 * 
		 * Default action
		 */
		function index()
		{	
			// get current and back view
			$currentView = $_REQUEST['view'];
			$this->assign("currentView", $currentView);
			$backTitle = $_REQUEST['back'];
			$this->assign("backTitle", $backTitle);
			
			// search button visible
			$this->assign("searchVisible", 1);
			
			// image and category path
			$this->assign("imagesPath", $this->loadResources('images'));
			$this->assign("categoryPath", $this->loadResources('category'));		
	        $this->assign("productPath", $this->loadResources('product'));
			
			// get current query
			$typeId = $_REQUEST['type'];
			$typeModel = $this->getModel('TypeModel');
			$type = $typeModel->getTypeById($typeId);
			if ($type == null)
				return;
				
			$this->assign("title", $type['name']);
			$this->assign('page', 'type');
			$this->assign('pageId', $type['id']);
			
			if ($type['style'] == 0){
				$categoryModel = $this->getModel('CategoryModel');
				$categories = $categoryModel->getCategoriesByQuery($type['query']);
				
				$this->assign("categories", $categories);
				$this->assign("categoriesCount", count($categories));
				
				$this->loadPageTemplate('category');
			}
			else if ($type['style'] == 1){
	            $productModel = $this->getModel('ProductModel');
	            $products = $productModel->getProductsByQuery($type['query']);
	            
	            $this->assign("products", $products);
	            $this->assign("products_count", count($products));
	            $this->assign("type", $type);

				$this->loadPageTemplate('product_list_page');
			}
		}
	}
?>